<?php
require_once 'sisco.php';

/**
 * @package sisco
 * @subpackage controller
 */
class Historico_Productos_protoweb extends Sisco
{

	public function __construct() 
	{
		parent::__construct();
	}

    protected function createPrepare(&$data)
    {
        
    
        // Collections of has one relationships
        $data['productos'] = new Producto();
        $data['productos']->get();
        $data['marcas'] = new Marca();
        $data['marcas']->get();
        $data['categorias'] = new Categoria();
        $data['categorias']->get();
        $data['origenes'] = new Origen();
        $data['origenes']->get();
        $data['medidas'] = new Medida();
        $data['medidas']->get();
        $data['usuarios'] = new Usuario();
        $data['usuarios']->get();
    
        // Collections of has many to many relationships
    
    }

    protected function assignCreatePost(&$data)
    {
        if ( array_key_exists('codigo_interno', $this->input->post()) ) $data['historico_producto']->codigo_interno = $this->input->post('codigo_interno');
    
        // has one relationships
        if ($this->input->post('producto')) {
            $data['producto'] = new Producto($this->input->post('producto'));
        }
        if ($this->input->post('marca')) {
            $data['marca'] = new Marca($this->input->post('marca'));
        }
        if ($this->input->post('categoria')) {
            $data['categoria'] = new Categoria($this->input->post('categoria'));
        }
        if ($this->input->post('origen')) {
            $data['origen'] = new Origen($this->input->post('origen'));
        }
        if ($this->input->post('medida')) {
            $data['medida'] = new Medida($this->input->post('medida'));
        }
        if ($this->input->post('usuario')) {
            $data['usuario'] = new Usuario($this->input->post('usuario'));
        }
        
        // Many to many
    
        return true;
    }

    protected function afterCreateSuccess(&$data)
    {
        
        
    }

    protected function revertOnCreateError(&$data)
    {
        
        
    }

    protected function updatePrepare(&$data)
    {
        
    
        // Collections of has one relationships
        $data['productos'] = new Producto();
        $data['productos']->get();
        $data['marcas'] = new Marca();
        $data['marcas']->get();
        $data['categorias'] = new Categoria();
        $data['categorias']->get();
        $data['origenes'] = new Origen();
        $data['origenes']->get();
        $data['medidas'] = new Medida();
        $data['medidas']->get();
        $data['usuarios'] = new Usuario();
        $data['usuarios']->get();
        
        // Collections of has many to many relationships
    
    }

    protected function assignUpdatePost(&$data)
    {
        if ( array_key_exists('codigo_interno',$this->input->post()) ) $data['historico_producto']->codigo_interno = $this->input->post('codigo_interno');
    
        // has one relationships
        if ($this->input->post('producto')) {
            $data['producto'] = new Producto($this->input->post('producto'));
        }

        if ($this->input->post('marca')) {
            $data['marca'] = new Marca($this->input->post('marca'));
        }

        if ($this->input->post('categoria')) {
            $data['categoria'] = new Categoria($this->input->post('categoria'));
        }

        if ($this->input->post('origen')) {
            $data['origen'] = new Origen($this->input->post('origen'));
        }

        if ($this->input->post('medida')) {
            $data['medida'] = new Medida($this->input->post('medida'));
        }

        if ($this->input->post('usuario')) {
            $data['usuario'] = new Usuario($this->input->post('usuario'));
        }

    
        // Many to many
        
    
        return true;
    }

    protected function afterUpdateSuccess(&$data)
    {
        
        
    }

    protected function revertOnUpdateError(&$data)
    {
        
        
    }

    protected function afterDeleteSuccess(&$data)
    {
        
        
    }

    protected function readPrepare(&$data)
    {
        
        
    }

    protected function saveEntity(&$data,$saveHasOne=true,$saveHasMany=true)
    {
        // saving attributes
        if ( ! $data['historico_producto']->save()) {
            return false;
        }

        // saving one raleted
        if ($saveHasOne) {
            $relationships = array();
                if (array_key_exists('producto', $data)) {
                array_push($relationships, $data['producto']);
            }
                if (array_key_exists('marca', $data)) {
                array_push($relationships, $data['marca']);
            }
                if (array_key_exists('categoria', $data)) {
                array_push($relationships, $data['categoria']);
            }
                if (array_key_exists('origen', $data)) {
                array_push($relationships, $data['origen']);
            }
                if (array_key_exists('medida', $data)) {
                array_push($relationships, $data['medida']);
            }
                if (array_key_exists('usuario', $data)) {
                array_push($relationships, $data['usuario']);
            }
            
            if ( ! $data['historico_producto']->save($relationships)) {
                return false;
            }
        }

        // saving many to many relationships
        if ($saveHasMany) {
                    }
        return true;
    }

        
    
} // class Historico_Productos

/* End of file historico_productos_protoweb.php */
/* Location: ./application/controllers/historico_productos_protoweb.php */